package com.address.service.impl;

import java.io.File;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import com.address.entity.Contact;
import com.address.entity.Status;
import com.address.service.ConcatService;
import com.address.util.FileUtils;

public class ConcatServiceImpl implements ConcatService {

	public String insert(Contact contact) {
		String status = Status.SUCCESS;
		Map<String, String> messages = initMessages(contact);		
		try{
			FileUtils.writeAsUTF8(contact.getFileName(), messages);
			status = Status.SUCCESS;
		}catch(Exception e){
			e.printStackTrace();
			status = Status.FAILURE;
		}
		return status;
	}

	private Map<String, String> initMessages(Contact contact) {
		Map<String, String> messages = new LinkedHashMap<String, String>();
		
		if (contact.getAddress() != null) {
			messages.put(Contact.ADDRESS, contact.getAddress());
		}
		if (contact.getEmail() != null) {
			messages.put(Contact.EMAIL, contact.getEmail());
		}
		if (contact.getName() != null) {
			messages.put(Contact.NAME, contact.getName());
		}
		if (contact.getSex() != null) {
			messages.put(Contact.SEX, contact.getSex());
		}
		if (contact.getTelephone() != null) {
			messages.put(Contact.TELEPHONE, contact.getTelephone());
		}
		messages.put(Contact.ID, contact.getId());
		
		return messages;
	}

	public String delete(String id) {
		String status = Status.ERROR;
		List<File> files = FileUtils.getAllLocalProperties();
		for(File file : files){
			if(FileUtils.getFileNameWithoutExtension(file).equals(id)){
				if(FileUtils.getValueByKey(file, Contact.ID) == null || FileUtils.getValueByKey(file, Contact.ID).equals(id)){
					file.delete();
					status = Status.SUCCESS;
				}else{
					status = Status.FAILURE;
				}
			}
		}
		return status;
	}

	public String update(Contact concat) {
		ConcatService concatService = new ConcatServiceImpl();
		String status = Status.FAILURE;
		try{
			concatService.delete(concat.getId());
			concatService.insert(concat);
			status = Status.SUCCESS;
		}catch(Exception e){
			e.printStackTrace();
			status = Status.FAILURE;
		}
		return status;
		
	}

	public List<Contact> queryAll() {
		List<File> files = FileUtils.getAllLocalProperties();
		List<Contact> contacts= new LinkedList<Contact>();
		for(File f : files){
			Contact contact = new Contact();
			fillContacts(f,contact);	
			contacts.add(contact);
		}	
		return contacts;
	}
	
	

	private void fillContacts(File f, Contact contact) {
			contact.setAddress(FileUtils.getValueByKey(f, Contact.ADDRESS));
			contact.setEmail(FileUtils.getValueByKey(f, Contact.EMAIL));
			contact.setName(FileUtils.getValueByKey(f, Contact.NAME));
			contact.setSex(FileUtils.getValueByKey(f, Contact.SEX));
			contact.setTelephone(FileUtils.getValueByKey(f, Contact.TELEPHONE));
	
	}

	public List<Contact> query(Contact concat) {
		ConcatService concatService = new ConcatServiceImpl();
		List<Contact> contacts = concatService.queryAll();
		List<Contact> result = new LinkedList<Contact>();
		for(Contact c : contacts){
			if(c.contains(concat)){
				result.add(c);
			}
		}
		return result;
	}

	

}
